﻿using System;
using System.Collections;
using System.Collections.Generic;
using LogSystem;
using Pathfinding;
using Pathfinding.RVO;
using UnityEngine;
using UnityEngine.Serialization;
using RootMotion.Dynamics;
using Yoozoo.Gameplay.City;
using Yoozoo.Gameplay.Liberty.Pool;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.Liberty.AI
{
    public class HittableBottle : MonoBehaviour, IHittable
    {
        public static Action<int> OnKill;
        
        // 受击特效路径
        [SerializeField]
        private string _onHitEffectResPath;
        // 受击特效
        private GameObject _currentOnHitEffect;
        // 是否已经被打爆
        public AIEntity aiEntity;
        
        public void OnHit(AIEntity attackerEntity, int damageHp, AnimationState animationState, bool hitHard,int weaponId)
        {
            // Debug.Log($"HittableBottle {attackerEntity.data.uniqueId}");
            aiEntity.data.hp -= damageHp;
            showOnHitEffect();
            OnKill?.Invoke(attackerEntity.data.uniqueId);
        }

        public void OnGunHitPoint(Vector3 position, Vector3 normal)
        {
        }

        void showOnHitEffect()
        {
            hideOnHitEffect();
            
            LibertyPoolMgr.GetInstance().Pop(_onHitEffectResPath, go =>
            {
                // 隐藏瓶子，播放特效
                _currentOnHitEffect = go;
                _currentOnHitEffect.transform.position = transform.position;
                _currentOnHitEffect.transform.rotation = transform.rotation;
                _currentOnHitEffect.gameObject.SetActive(true);
                gameObject.SetActive(false);
            
                Timers.inst.Add(1f, 1, o =>
                {
                    hideOnHitEffect();
                });
            });
           
        }
        void hideOnHitEffect()
        {
            if (_currentOnHitEffect)
            {
                LibertyPoolMgr.GetInstance().Push(_onHitEffectResPath, _currentOnHitEffect);
                _currentOnHitEffect = null;
            }
        }
        
    }
}